//


#import "NT_MenuView.h"

//
@interface NT_MenuView ()<UITableViewDelegate, UITableViewDataSource>
/** <#Description#>*/
@property (nonatomic, strong) UITableView *tableView;


@end

// 获取主屏幕的高度
#define kMainScreenHeight                       ([UIScreen mainScreen].bounds.size.height)
// 获取主屏幕的宽度
#define kMainScreenWidth                        ([UIScreen mainScreen].bounds.size.width)

@implementation NT_MenuView

- (instancetype)init {
    if ([super init]) {
        self.frame = [[UIScreen mainScreen] bounds ];
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        [self addSubview:self.tableView];
        
    }return self;
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.center = cell.center;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont systemFontOfSize:17];
    cell.textLabel.text = _dataArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self tap];
    if (self.indexBlock) {
        self.indexBlock(indexPath.row);
    }
}
//
- (void)showView {
    //
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        //        self.frame =
    } completion:^(BOOL finished) {
    }];
    
}
//删除
- (void)tap {
    [self removeFromSuperview];
}
- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [_tableView reloadData];
    if (dataArray.count == 1) {
        _tableView.rowHeight  = 70;
    }else
        _tableView.rowHeight  = 35;
}

#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(kMainScreenWidth - 120, 69, 100, 70) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight  = (35);
        _tableView.userInteractionEnabled = YES;
        
    }
    return _tableView;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self tap];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
